package com.lemo.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {

	public static void main(String[] args) {
		Ticket ticket = new Ticket();

		new Thread(ticket, "1").start();
		new Thread(ticket, "2").start();
		new Thread(ticket, "3").start();
	}
}

class Ticket implements Runnable {

	private int ticket = 100;

	private Lock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {

			lock.lock();
			try {
				if (ticket > 0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					System.out.println(Thread.currentThread().getName() + ":余票为" + --ticket);
				}

			} finally {
				lock.unlock();
			}
		}
	}
}
